package view.listeners;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JRadioButton;
import javax.swing.JTextField;

import model.Constants;
import model.HostAddress;
import model.Subnet;
import view.components.BinaryIpTextArea;
import view.components.IPTextArea;
import view.components.SubnetComboBox;
import view.dialogs.AddSimpleIpDialog;
import view.dialogs.InfoDialog;
import view.views.dialogContentViews.AddSimpleIPContentPanel;
import controller.Calculator;
import controller.Controller;

/**
 * @author Alexander Müller
 * 
 */
public class AddIpListener implements ActionListener {
	
	private final Window comp;
	private final SubnetComboBox subnetBox;
	private final IPTextArea ipTextArea;
	private final JTextField textField;
	private final BinaryIpTextArea binaryTextArea;
	private final AddSimpleIPContentPanel content;

	public AddIpListener(AddSimpleIpDialog addSimpleIpDialog,
			AddSimpleIPContentPanel content) {
		this.comp = addSimpleIpDialog;
		this.subnetBox = content.getSubnetBox();
		this.ipTextArea = content.getIpTextArea();
		this.textField = content.getTextField();
		this.binaryTextArea = content.getBinaryIpTextArea();
		this.content = content;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		for (JRadioButton element : content.getButtons()) {
			if (element.isSelected()) {
				if (content.getButtons().indexOf(element) == 0) {

					if (Calculator.getInstance().validateIpTextArea(ipTextArea)) {

						String ip = Calculator.getInstance()
								.getIpFromIpTextArea(ipTextArea);

						addIp(ip);
					} else {
						new InfoDialog(Constants.ERROR, Constants.TOOMANYCHARS);
					}
				} else {

					if (Calculator.getInstance().validateBinaryIpTextArea(
							binaryTextArea)) {

						String ip = Calculator.getInstance()
								.getIpFromBinaryIpTextArea(binaryTextArea);
						addIp(ip);
					} else {
						new InfoDialog(Constants.ERROR, Constants.TOOMANYCHARS);
					}
				}
			}
		}



	}

	private void addIp(String ip) {
		String s = textField.getText();

		if (s.equals(Constants.EMPTY) || s.equals(null)) {
			s = Constants.IPRESERVED;
		}

		if (!subnetBox.getBox().getSelectedItem().equals(Constants.EMPTY)) {
			
			ArrayList<HostAddress> hostList = Controller.getInstance()
					.getHosts();
			int indexHost = 0, indexSub = 0;
			Subnet sub = subnetBox.getMap().get(
					subnetBox.getBox().getSelectedItem());
			for (HostAddress element : hostList) {
				for (Subnet subnet : element.getSubnetList()) {
					if (sub.getNetAdress().getAddress()
							.equals(subnet.getNetAdress().getAddress())) {
						if(hostList.get(indexHost).getSubnetList()
								.get(indexSub).editIpAddress(ip, s)){
						Controller.getInstance().setHosts(hostList);
						comp.dispose();
						}else {
							new InfoDialog(Constants.ERROR, Constants.IPADDINGERROR);
						
						}
					}
					indexSub++;
				}
				indexHost++;
				indexSub = 0;
			}

		}

	}
	}


